import glfw
import OpenGL.GL as gl
import glm
import imgui
from imgui.integrations.glfw import GlfwRenderer
import numpy as np

def run() -> None:
    if not glfw.init():
        print("初始化GLFW错误！")
        return
    glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 4)
    glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 5)
    glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
    window = glfw.create_window(800, 600, "你好，小不点！", None, None)
    if not window:
        print("创建窗口失败！")
        glfw.terminate()
        return
    glfw.set_window_pos(window, 100, 100)
    glfw.make_context_current(window)
    # 设置背景颜色
    background_color = glm.vec4(0.8, 0.8, 0.8, 1.0)
    # 初始化imgui
    imgui.create_context()
    impl = GlfwRenderer(window, False)
    # 设置显示中文，字体为黑体
    io = imgui.get_io()
    fonts = io.fonts
    cn_font = io.fonts.add_font_from_file_ttf("C:/Windows/Fonts/simhei.ttf", 
                16, None, fonts.get_glyph_ranges_chinese_full())
    impl.refresh_font_texture()
    # 初始化着色器，VAO等
    vertex_shader_code = """
    #version 450 core
    void main()
    {
        gl_Position = vec4(0.0, 0.0, 0.0, 1.0);
    }
    """
    fragment_shader_code = """
    #version 450 core
    out vec4 fragment_color;
    void main()
    {
        fragment_color = vec4(0.0, 0.0, 1.0, 1.0);
    }
    """    
    vertex_shader = gl.glCreateShader(gl.GL_VERTEX_SHADER)
    gl.glShaderSource(vertex_shader, vertex_shader_code)
    gl.glCompileShader(vertex_shader)
    fragment_shader = gl.glCreateShader(gl.GL_FRAGMENT_SHADER)
    gl.glShaderSource(fragment_shader, fragment_shader_code)
    gl.glCompileShader(fragment_shader)
    program = gl.glCreateProgram()
    gl.glAttachShader(program, vertex_shader)
    gl.glAttachShader(program, fragment_shader)
    gl.glLinkProgram(program)
    gl.glDeleteShader(vertex_shader)
    gl.glDeleteShader(fragment_shader)

    vao = np.empty(1, dtype=gl.GLuint)
    gl.glCreateVertexArrays(1, vao)
    point_size = 20.0

    while not glfw.window_should_close(window):
        gl.glClearNamedFramebufferfv(0, gl.GL_COLOR, 0, 
                                     background_color.to_tuple())
       # 渲染图形
        gl.glUseProgram(program)
        gl.glBindVertexArray(vao[0])
        gl.glPointSize(point_size)
        gl.glDrawArrays(gl.GL_POINTS, 0, 1)
        # 显示imgui界面
        impl.process_inputs()
        imgui.new_frame()
        imgui.push_font(cn_font)
        imgui.set_next_window_position(0, 0, condition=imgui.FIRST_USE_EVER)
        imgui.set_next_window_size(300, 180, condition=imgui.FIRST_USE_EVER)
        imgui.begin("改变点的大小")
        _, new_size = imgui.slider_float("点的大小", point_size, 2.0, 80.0)
        point_size = new_size
        imgui.end()
        imgui.pop_font()
        imgui.render()
        impl.render(imgui.get_draw_data())
        glfw.swap_buffers(window)
        glfw.poll_events()

    # 4. 清除资源
    gl.glDeleteVertexArrays(1, vao[0])
    gl.glDeleteProgram(program)
    impl.shutdown()
    glfw.destroy_window(window)
    glfw.terminate()

if __name__ == "__main__":
    run()
